<?php
namespace app\index\controller;

use Endroid\QrCode\QrCode;
use lib\test1;
use think\App;
use think\cache\driver\Redis;
use think\Controller;

use think\facade\Cache;
use think\facade\Env;
use think\Loader;
use think\Validate;

class Index extends Controller
{

   public function ping()
   {
       set_time_limit(0);

       $host = '192.168.10.122';
       $port = 22;

       $conn = ssh2_connect($host, $port);

       ssh2_auth_password($conn, 'root', '123');

       //执行命令
       $cmd = 'ping baidu.com >> /test/cmd.txt';
       ssh2_exec($conn, $cmd);

   }

   public function get_ping()
   {

       $time = time();
       set_time_limit(0);

       $host = '192.168.10.122';
       $port = 22;

       $conn = ssh2_connect($host, $port);
       ssh2_auth_password($conn, 'root', '123');
       $sftp = ssh2_sftp($conn);
       $data = file_get_contents("ssh2.sftp://$sftp/test/cmd.txt");

       echo json_encode(['code' => 200, 'data' => $data]);die;
   }

    public function html()
    {
        return $this->fetch('html');
    }

    public function testFileConent()
    {
        $data = file_get_contents('1.txt');
        dump($data);die;
    }

    /**
     * 首页
     */
    public function uploadView()
    {
        return $this->fetch('index');
        //phpinfo();die;
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    /**
     * 文件上传
     */
    public function upload()
    {

        $file = $_FILES;

        $fileOldName = $_FILES['file']['name'];

        if(empty($fileOldName)){
            $this->error('请上传文件','index/index/index');
        }

        $dirPath = './public/uploads/';
        $res = move_uploaded_file($_FILES['file']['tmp_name'], $dirPath.$file['file']['name']);

        if(!$res){
            $this->error('文件上传失败!','index/index/index');
        }

        $this->success('文件上传成功!','index/index/index');

    }

    /**
     * 文件列表
     */
    public function index()
    {
        echo 'this is tp5';die;

        $Path = './public/uploads/';
        $list = scandir($Path);

        $data = array();

        foreach ($list as $k => $v){

            if($v == '.' || $v == '..'){
                continue;
            }

            $ctime_arr[] = fileatime($Path.$v);
            $tmp['name'] = $Path.$v;
            $tmp['ctime'] = date('Y-m-d H:i:s', fileatime($Path.$v));
            $tmp['atime'] = fileatime($Path.$v);
            $tmp['down_name'] = $v;

            $data[] = $tmp;
        }

        array_multisort($ctime_arr, SORT_DESC ,$data);

        $this->assign('data', $data);

        return $this->fetch('list');
    }

    /**
     * 执行命令界面
     */
    public function command()
    {
        return $this->fetch();
    }

    /**
     *  执行命令
     */
    public function finish()
    {
        $content = input('content');

        if(!$content){
            $this->error('请输入命令!');
        }

        exec($content);

        $this->success('success','index/index/index');
    }


}
